<?php
declare (strict_types = 1);

namespace app\controller\api;

use app\BaseController;
use app\model\Partner as PartnerModel;
use think\facade\Session;

class Partner extends BaseController
{
    protected function initialize()
    {
        parent::initialize();
        // 检查管理员登录状态
        if (!Session::has('admin_id')) {
            return json(['code' => 401, 'msg' => '请先登录']);
        }
    }
    
    /**
     * 获取合伙人列表
     */
    public function index()
    {
        $page = input('page/d', 1);
        $limit = input('limit/d', 10);
        $status = input('status/d', -1);
        
        $where = [];
        if ($status >= 0) {
            $where['status'] = $status;
        }
        
        $list = PartnerModel::getPageList($where, $page, $limit);
        return json(['code' => 0, 'data' => $list]);
    }
    
    /**
     * 添加合伙人
     */
    public function add()
    {
        $data = input('post.');
        
        // 验证手机号是否存在
        if (PartnerModel::where('phone', $data['phone'])->find()) {
            return json(['code' => 1, 'msg' => '手机号已存在']);
        }
        
        // 验证身份证号是否存在
        if (PartnerModel::where('id_card', $data['id_card'])->find()) {
            return json(['code' => 1, 'msg' => '身份证号已存在']);
        }
        
        $partner = new PartnerModel;
        $partner->save($data);
        
        return json(['code' => 0, 'msg' => '添加成功']);
    }
    
    /**
     * 编辑合伙人
     */
    public function edit()
    {
        $data = input('post.');
        
        // 验证手机号是否存在
        $partner = PartnerModel::where('phone', $data['phone'])
            ->where('id', '<>', $data['id'])
            ->find();
        if ($partner) {
            return json(['code' => 1, 'msg' => '手机号已存在']);
        }
        
        // 验证身份证号是否存在
        $partner = PartnerModel::where('id_card', $data['id_card'])
            ->where('id', '<>', $data['id'])
            ->find();
        if ($partner) {
            return json(['code' => 1, 'msg' => '身份证号已存在']);
        }
        
        PartnerModel::update($data);
        return json(['code' => 0, 'msg' => '编辑成功']);
    }
    
    /**
     * 删除合伙人
     */
    public function delete()
    {
        $id = input('id/d');
        if (!$id) {
            return json(['code' => 1, 'msg' => '参数错误']);
        }
        
        PartnerModel::destroy($id);
        return json(['code' => 0, 'msg' => '删除成功']);
    }
    
    /**
     * 审核合伙人
     */
    public function audit()
    {
        $id = input('id/d');
        $status = input('status/d');
        $remark = input('remark/s', '');
        
        if (!$id || !in_array($status, [1, 2])) {
            return json(['code' => 1, 'msg' => '参数错误']);
        }
        
        $partner = PartnerModel::find($id);
        if (!$partner) {
            return json(['code' => 1, 'msg' => '合伙人不存在']);
        }
        
        if ($partner->status != 0) {
            return json(['code' => 1, 'msg' => '该合伙人已审核']);
        }
        
        $partner->status = $status;
        $partner->remark = $remark;
        $partner->save();
        
        return json(['code' => 0, 'msg' => '审核成功']);
    }
} 